function initEcharts(dom) {
    let temp = echarts.init(dom, "dark");
    return temp;
}
let leftTop = $("#left-top");//jquery对象
let cov19Map = $("#cov19-map");//jquery对象
let rightTop = $("#right-top");//jquery对象
let rightBottom = $("#right-bottom");//jquery对象
let leftTopChart = initEcharts(leftTop[0]);
let cov19MapChart = initEcharts(cov19Map[0]);
let rightTopChart = initEcharts(rightTop[0]);
let rightBottomChart = initEcharts(rightBottom[0]);

async function loadLeftData() {
    leftTopChart.setOption({
        tooltip: {

        },
        legend: {

        },
        xAxis: {
            type: "category"
        },
        yAxis: {

        },
        series: [
            {
                type: 'line'
            },
            {
                type: 'line'
            },
            {
                type: 'line'
            }
        ]

    });

    let res = await $.ajax({
        url: "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list",
        method: "post",
        data: {
            modules: "chinaDayList,chinaDayAddList,cityStatis,nowConfirmStatis,provinceCompare"
        },
        dataType: "json",
    }).promise();
    let arrs = [];//构建数据集中的对象数组
    let myData = res.data.chinaDayList;//数组
    myData.forEach(function (item, index) {
        arrs.push({
            name: item.date,
            "新增确诊": item.nowConfirm,
            "治愈": item.heal,
            "死亡": item.dead
        });
    });
    leftTopChart.setOption({
        yAxis: {
            axisLabel: {
                formatter: function (val) {
                    let num = parseInt(val / 10000);
                    return num > 0 ? num + "w" : num;
                }
            }
        },
        dataset: {
            source: arrs
        }
    });

}

async function loadMapData() {
    cov19MapChart.setOption({
        visualMap: {
            type: "piecewise",//分段数据映射
        },
        series: [{
            type: 'map',//地图
            map: 'china',//注册地图的名称
            label: {
                show: true
            }

        }]
    });


    // https://voice.baidu.com/api/newpneumonia?from=page&callback=jsonp_1661034366228_74305
    let res = await $.ajax({
        url: "https://voice.baidu.com/api/newpneumonia",
        method: "get",
        data: {
            from: "page"
        },
        dataType: "jsonp"
    }).promise();
    let provinces = res.data.caseList;//包含所有的省份对象数组
    let mapData = [];//构建地图中的数据结果
    provinces.forEach(function (item, index) {
        mapData.push({
            name: item.area,
            value: item.confirmed
        });
    });
    cov19MapChart.setOption({
        tooltip: {
            formatter: "{b}<br>累计确诊:{c}",
        },
        visualMap: {
            bottom: 30,
            text: ['人数'],
            showLabel: true,//是否显示每项的文本标签
            textStyle: {
                color: "#fff"
            },
            pieces: [
                { min: 100000 },
                { min: 10000, max: 100000 },
                { min: 1000, max: 10000 },
                { min: 100, max: 1000 },
                { min: 0, max: 100 }
            ]
        },
        series: [{

            data: mapData

        }]
    });
}
async function loadRightTopData() {
    rightTopChart.setOption({
        legend: {

        },
        tooltip: {

        },
        xAxis: {
            type: "category"
        },
        yAxis: {
            axisLabel: {
                formatter: function (val) {
                    let num = parseInt(val / 10000);

                    return num > 0 ? num + "W" : num;
                }
            }
        },

        series: [
            {
                type: 'bar'
            },
            {
                type: 'bar'
            },
            {
                type: 'bar'
            }
        ]
    });
    let res = await $.ajax({
        url: "https://voice.baidu.com/api/newpneumonia",
        method: "get",
        data: {
            from: "page"
        },
        dataType: "jsonp"
    }).promise();
    let provinces = res.data.caseList;//包含所有的省份对象数组
    let provinceData = [];//构建展示的数据
    provinces.forEach(function (item, index) {
        provinceData.push({
            name: item.area,
            "确诊": item.confirmed,
            "死亡": item.died,
            "治愈": item.crued
        });
    });
    // console.log(provinceData);//34条
    let proNum = 5;
    let nowData = provinceData.slice(0, proNum);//第一次的时候里面有前5条数据  [0,5] 包含左边界，不包含右边界
    rightTopChart.setOption({
        dataset: {
            source: nowData
        }
    });

    setInterval(function () {
        if (proNum == 33) {
            proNum = 0;
        } else {
            proNum = proNum + 1;
        }
        nowData.shift();//删除数组中第一个元素
        nowData.push(provinceData[proNum]);//删除数组中的第一个之后，立马添加一个
        rightTopChart.setOption({
            dataset: {
                source: nowData
            }
        });

    }, 1500);

}
async function loadRightBottomData() {
    rightBottomChart.setOption({
        title: {

        },
        tooltip: {},
        legend: {//数据交互按钮
            orient:"vertical",
            left:30
        },

        series: [
            {
                name: '',
                type: 'pie',
                // left:400,
                // center: [200, 280],//圆心
                radius: [0, 80],//半径，内半径和外半径

            }
        ]
    });

    let res = await $.ajax({
        url: "https://voice.baidu.com/api/newpneumonia",
        method: "get",
        data: {
            from: "page"
        },
        dataType: "jsonp"
    }).promise();
    let provinces = res.data.caseList;//包含所有的省份对象数组
    // console.log(provinces);
    let overSea=[];
    provinces.forEach(function(item,index){
        overSea.push({
            name:item.area,
            value:item.overseasInputRelative*1
        });
    });

    overSea.sort(function(a,b){
        return b.value - a.value;
    });
    overSea.length=5;
    rightBottomChart.setOption({
        dataset:{
            source:overSea
        }
    });

}
loadLeftData();
loadMapData();
loadRightTopData();
loadRightBottomData();
window.onresize = function () {
    leftTopChart.resize();
    cov19MapChart.resize();
    rightTopChart.resize();
};